Member Comments
No comments so far
Continuing our discussion on how to program AHK scripts, we’ll talk about some of the basics behind variables and ex
With the exception of local variables (which are accessed or created inside a function), all variables are global and their contents may be read or altered by any part of a script you create. Remember that except where noted on the functions page, variables are not declared and only are created by using them.
Variable names in AHK scripts are not case sensitive, so something like "PokerTableName" is the same as "pokertablename." All variable names can be up to 253 characters long and can be numbers, letters, and allowed punctuation characters. Style conventions within the AHK community call for variables to be named only using letters, numbers, and the underscore character, so something like "Mouse_Position" is fine, while "Mouse[Position]" isn’t.
Going this route will allow others to take a look at your script if you run into a problem and understand the variables you used. Although a variable can just be numbers, the style is typically reserved for incoming command line parameters. Also, the words “and,” “or,” and “not” should not be used in variable names.
To store a string or number inside a variable, you can use either the traditional or ex
SomeNumber = 431
SomeString = “This is a literal string.”
CopyOfVar = %Var%
On the other hand, you can use the ex
SomeNumber := 552
SomeString := “This is another literal string.”
CopyOfVar := Var
You’ll notice that with this operator, the percent signs are not used. Now, as you might have surmised, there are two different ways to erase the contents of a variable.
MyVar =
MyVar := “”
The empty quotes are only used with the ex
Now, let’s say you wanted to retrieve the contents of variables you create. Like the two ways of storing variables, there are two ways of retrieving them. The traditional method requires that each variable name be enclosed in percent signs when you want to retrieve the contents. An example:
MsgBox The value in the variable named Var is %Var%
CopyOfVar = %Var%
The ex
MsgBox % “The value in the variable named Var is “ . Var . “.”
CopyOfVar := Var
In the MsgBox line for the ex
At this point, we’ll move our discussion to ex
if (CurrentSetting > 100 or SomeColor <> “Green”)
MsgBox The setting is too high or the wrong color is up.
In this example, “Green” is in quotes because it is a literal string. To put in an actual quote-character inside a literal string, specify two consecutive quotes as shown twice in this example: “Barry said, “”For every bad beat.”””
Note that an if-statement containing an ex
To specify an empty string inside an ex
To assign a result to a variable, use the := operator. Here’s an example:
NetProfit := Profit * (1 – Gross/100)
When an ex
Inside an ex
This gives you some background on what it takes to be an AHK script programmer. Understanding how variables and ex
No comments so far